package com.bsdsma.fengheguaiapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.bsdsma.fengheguaiapp.dto.RoomAddDto;
import com.bsdsma.fengheguaiapp.dto.RoomSearchDto;
import com.bsdsma.fengheguaiapp.dto.DataGroupByDateDto;
import com.bsdsma.fengheguaiapp.entity.Room;
import com.bsdsma.fengheguaiapp.enums.Role;
import com.bsdsma.fengheguaiapp.mapper.RoomMapper;
import com.bsdsma.fengheguaiapp.service.RoomService;
import com.bsdsma.fengheguaiapp.constants.ErrorCodes;
import com.bsdsma.fengheguaiapp.dto.PageRetDto;
import com.bsdsma.fengheguaiapp.exceptionhandler.GlobalRuntimeException;
import com.bsdsma.fengheguaiapp.utils.CustomerBeanUtils;
import com.bsdsma.fengheguaiapp.utils.DateUtils;
import com.bsdsma.fengheguaiapp.utils.PageRetUtils;
import com.bsdsma.fengheguaiapp.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class RoomServiceImpl implements RoomService {

    /**
     * 使用Spring注入Mybatis操作对象
     */
    @Autowired
    private RoomMapper mapper;

    @Override
    public PageRetDto<Room> search(RoomSearchDto dto) {
        Room bean = new Room();
        bean.setStatus(0);
        QueryWrapper<Room> wrapper = new QueryWrapper<>(bean);

        if (!StringUtils.isBlank(dto.getRoomType())) {
            wrapper.like("roomType", "%" + dto.getRoomType() + "%");
        }
        if (null != dto.getPrice()) {
            wrapper.eq("price", dto.getPrice());
        }
        if (null != dto.getRoomNum()) {
            wrapper.eq("roomNum", dto.getRoomNum());
        }
        IPage<Room> pageData = mapper.selectPage(new PageDTO<>(dto.getPage(), dto.getSize()), wrapper);
        return PageRetUtils.parse(pageData);
    }

    @Override
    public List<Room> listAll() {
        Room bean = new Room();
        bean.setStatus(0);
        return mapper.selectList(new QueryWrapper<>(bean));
    }

    @Override
    public Room add(RoomAddDto dto) {
        Room bean = CustomerBeanUtils.copyPropertiesClass(dto, Room.class);
        String roomType = dto.getRoomType();
        Integer price = dto.getPrice();
        Integer roomNum = dto.getRoomNum();

        if (StringUtils.isBlank(roomType)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写房间类型");
        }
        if (null == price) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写房间价格");
        }
        if (null == roomNum) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写可住人数");
        }
        bean.setStatus(0);
        bean.setCt(DateUtils.getNow());
        bean.setUt(DateUtils.getNow());
        mapper.insert(bean);
        return bean;
    }

    @Override
    public Room update(Long id, RoomAddDto dto) {
        Room bean = this.getById(id);
        if (null == bean) {
            throw new GlobalRuntimeException(ErrorCodes.OBJ_NOT_EXISTS, "房间种类不存在");
        }
        String roomType = dto.getRoomType();
        Integer price = dto.getPrice();
        Integer roomNum = dto.getRoomNum();

        if (StringUtils.isBlank(roomType)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写房间类型");
        }
        bean.setRoomType(roomType);
        if (null == price) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写房间价格");
        }
        bean.setPrice(price);
        if (null == roomNum) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写可住人数");
        }
        bean.setRoomNum(roomNum);
        mapper.updateById(bean);
        return bean;
    }

    @Override
    public Room getById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public void delete(Long id) {
        Room bean = this.getById(id);
        if (null == bean) {
            throw new GlobalRuntimeException(ErrorCodes.OBJ_NOT_EXISTS, "房间种类不存在");
        }
        bean.setStatus(-1);
        bean.setUt(DateUtils.getNow());
        mapper.updateById(bean);
    }

    @Override
    public int updateById(Room bean) {
        return mapper.updateById(bean);
    }

    @Override
    public Long count() {
        Room bean = new Room();
        bean.setStatus(0);
        return mapper.selectCount(new QueryWrapper<>(bean));
    }

    @Override
    public List<DataGroupByDateDto> countByDay(Date beginDate, Date endDate) {
        return mapper.countByDay(beginDate, endDate);
    }
}